package failover

import (
	"context"
	"errors"
	"gitee.com/DonHz/basic-go/webook/internal/service/sms"
	"log"
)

type FailoverSMSService struct{
	svcs []sms.Service
}

func NewFailoverSMSService(svcs []sms.Service) sms.Service {
	return &FailoverSMSService{
		svcs: svcs,
	}
}

// 轮询
func (f *FailoverSMSService) Send(ctx context.Context, tpl string, args []string, numbers ...string) error {
	for _, svc:=range f.svcs {
		err:= svc.Send(ctx, tpl, args, numbers...)
		// 发送成功
		if err==nil {
			return nil
		}
		// 输出日志，做好监控
		log.Println(err)
	}
	return errors.New("发送失败，所有服务商都尝试了一遍，大概率是机房网络崩了")
}